<?php
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */
/**
 * Generate Fields for Forms class.
 * @author frederic
 *
 */
class Fields{
	private static $count=0;
	private $tabindex=0;
	
	/**
	 * create a formular input type text.
	 * @param $type = text, password, checkbox
	 * @param $entity
	 * @param $name
	 * @param $size
	 * @param $defaultValue
	 * @param $options=array(
	 * 			'styleclass'=>"",	a CSS class for this field
	 * 			'label'=>"",		A label for the field
	 * 			'label-class'=>"",	a CSS class for this label
	 * 			'tooltip'=>"",		a tooltip to be display into the field tooltip. 
	 * 			'help'=>"",			a tooltip to be display after field.
	 * 			'help-class'=>"",	a CSS class for this help
	 * 			'accesskey'=>"[KEY]")
	 * @return unknown_type
	 */
	public function input($type="text", $entity="", $name="",$size="20",$defaultValue="", 
							$options=array()){
		if($name=="") $name=$type."_".$count++;
		if(empty($options['styleclass'])){
			$options['styleclass']="";
		}
		echo Forms::startFieldDelimiter(($options['styleclass']!=""?$options['styleclass']:""));
		if($options['label']!="" && $options['accesskey']){
			$options['label'] = ereg_replace($options['accesskey'],"<u>".$options['accesskey']."</u>",$options['label']);
		}
		if(empty($options['label-class'])){
			$options['label-class']="";
		}
		if($options['label']!="") echo "<div class=\"".($options['label-class']!=""?$options['label-class']:"label")."\"><label for=\"id_$entity.$name\">".$options['label'];
		if($options['label']!="") echo "</label></div>";
		if($type=="select"){
			echo "<select class=\"field\" id=\"id_".$entity."_".$name."\" name=\"".$entity."_".$name."\" size=\"$size\"".
					($options['tooltip']!=""?" title=\"".$options['tooltip']."\"":"").
					" value=\"$defaultValue\"".
					($options['accesskey']!=""?" accesskey=\"".$options['accesskey']."\"":"").
					" onfocus=\"this.className='setfocus';this.value='';\"".
					" onblur=\"this.className='lostfocus';if(this.value==\"\"){this.value='$defaultValue';}\"".
					">";
			echo "</select>";
		}else{
			echo "<input type=\"$type\" class=\"field\" id=\"id_".$entity."_".$name."\" name=\"".$entity."_".$name."\" size=\"$size\"".
					($options['tooltip']!=""?" title=\"".$options['tooltip']."\"":"").
					" value=\"$defaultValue\"".
					($options['accesskey']!=""?" accesskey=\"".$options['accesskey']."\"":"").
					" onfocus=\"this.className='setfocus'\"".
					" onblur=\"this.className='lostfocus'\"".
					" />";
		}
		if(empty($options['help-class'])){
			$options['help-class']="";
		}
		if($options['help']!="") echo "<div class=\"".($options['help-class']!=""?$options['help-class']:"help")."\">".$options['help']."</div>";
		echo Forms::endFieldDelimiter();
	}
	
	/**
	 * Create a Text input filed.
	 * @param $entity
	 * @param $name
	 * @param $size
	 * @param $defaultValue
	 * @param $options
	 * @return unknown_type
	 */
	public function text($entity="", $name="",$size="20",$defaultValue="", 
							$options=array()){
		self::input("text", $entity, $name,$size,$defaultValue,$options);
	}

	/**
	 * Create a password field into the formular.
	 * @param $entity
	 * @param $name
	 * @param $size
	 * @param $defaultValue
	 * @param $options
	 * @return unknown_type
	 */
	public function password($entity="", $name="",$size="10",$defaultValue="", 
							$options=array()){
		self::input("password", $entity, $name,$size,$defaultValue, 
							$options);
	}
	/**
	 * Create a file field into the formular.
	 * @param $entity
	 * @param $name
	 * @param $size
	 * @param $defaultValue
	 * @param $options
	 * @return unknown_type
	 */
	public function file($entity="", $name="",$size="10",$defaultValue="", 
							$options=array()){
		self::input("file", $entity, $name,$size,$defaultValue, 
							$options);
	}
	
	
	public function image($entity="", $name="",$size="10",$defaultValue="", 
							$options=array()){
		
		
	}
	
	/**
	 * start a new formular FieldSet. 
	 * @param $name
	 * @param $legend
	 * @return unknown_type
	 */
	public function startGroup($name,$options=array()){
		if(empty($options['styleclass'])){
			$options['styleclass']="";
		}
			echo "<fieldset".($options['styleclass']!=""?" class=\"".$options['styleclass']."\"":"").">";
		if(!empty($options['legend']) && !empty($options['accesskey'])){
			$options['legend'] = ereg_replace($options['accesskey'],"<u>".$options['accesskey']."</u>",$options['legend']);
		}		
		if($options['legend']!="") echo "<legend>".$options['legend']."</legend>";
	}
	/**
	 * End of a formular fieldset.
	 * @param $name
	 * @return unknown_type
	 */
	public function endGroup($name){
		echo "</fieldset>";
	}
		
	/**
	 * Generate a Submit button for a formular.
	 * @param $label
	 * @param $options
	 * @return unknown_type
	 */
	public function submit($label="Send",$options=array()){
		echo "<input type=\"submit\" ".
			 ($options['styleclass']!=""?" class=\"".$options['styleclass']."\"":"").
			 ($options['accesskey']!=""?" accesskey=\"".$options['accesskey']."\"":"").
			 ($options['title']!=""?" title=\"".$options['title']."\"":"").
			 "value=\"$label\" />";
		
	}
	/**
	 * Add a separator into the formular flux.
	 * @return unknown_type
	 */
	public function separator(){
		echo "<hr size=\"1\" class=\"separator\" />";
	}
}
?>